package com.rrd.dw.mr.gzip.kafka;

import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import com.rrd.dw.utils.DateUtil;
import com.rrd.dw.utils.MD5Util;
import com.ucredit.babel.common.model.Job;
import com.ucredit.babel.common.model.alibaba2.AliAssetsInfo;
import com.ucredit.babel.common.model.alibaba2.AliPayInfo;
import com.ucredit.babel.common.model.alibaba2.AliPayOrder;
import com.ucredit.babel.common.model.alibaba2.AliPayRecord;
import com.ucredit.babel.common.model.alibaba2.MessageType;
/**
 * 解析阿里支付、交易行信息
 * @author liubaoxin
 *
 */
@Deprecated
public class AliPayParse {
	public static final String CRT001 =  "\001";
	public static final Pattern CRT001_PATTERN =  Pattern.compile("\001");
	private  Text val = new Text();
	private  Text key= new Text();
	private  String user_id="";
	private String timestamstr="";
	private String job_id ="";
	private String account_name ="";
	private StringBuilder sb = new StringBuilder(500);
	
	/**
	 * 解析阿里支付基本信息，job信息
	 * @param call
	 * @throws InterruptedException 
	 * @throws IOException 
	 */
	public  void parseAliPayBase(Context context,AliPayRecord aliPay,String successful,String dataJson) throws IOException, InterruptedException{
		
		StringBuilder sb = new StringBuilder(200);
    	Job job = aliPay.getJob();
    	AliPayInfo payinfo= aliPay.getAlipay();
    	AliAssetsInfo asset = aliPay.getAssets();
    	MessageType messageType = aliPay.getMessType();
    	
    	if(payinfo==null ){
    		payinfo = new AliPayInfo();
    	}
    	if(asset==null ){
    		asset = new AliAssetsInfo();
    	}
    	if(messageType==null ){
    		messageType = new MessageType();
    	}
    	 this.timestamstr=job.getStartTime()==null?"":DateUtil.formateDateStr(job.getStartTime());
    	 this.job_id = job.getJobID()==null?"":job.getJobID();
    	 this.account_name = payinfo.getAccountName()==null?"":payinfo.getAccountName();
    	//主键,身份证+job_id+时间
    	sb.delete(0,sb.length());
//    	this.user_id = MD5Util.getMd5(timestamstr+job.getJobID()+(payinfo.getAccountName()==null?"":payinfo.getAccountName()));
    	sb.append(timestamstr).append(CRT001);
    	//job_id
    	sb.append(this.job_id).append(CRT001);
    	//支付宝账号
    	sb.append(this.account_name).append(CRT001);
    	//支付宝认证的真实名字
    	sb.append(payinfo.getRealName()==null?"":payinfo.getRealName()).append(CRT001);
    	//实名认证状态
    	sb.append(payinfo.getAuthStatus()==null?"":payinfo.getAuthStatus()).append(CRT001);
    	//身份证号
    	sb.append(payinfo.getIdno()==null?"":payinfo.getIdno()).append(CRT001);
    	//手机号码
    	sb.append(payinfo.getMobile()==null?"":payinfo.getMobile()).append(CRT001);
    	//注册日期
    	sb.append(payinfo.getRegisterTime()==null?"":DateUtil.formateDateStr(payinfo.getRegisterTime())).append(CRT001);
    	//上次登录时间
    	sb.append(payinfo.getLastLoginTime()==null?"":DateUtil.formateDateStr(payinfo.getLastLoginTime())).append(CRT001);
    	//来源用户手机号
    	sb.append(payinfo.getSourceUserMobile()==null?"":payinfo.getSourceUserMobile()).append(CRT001);
    	//来源用户身份证号
    	sb.append(payinfo.getSourceUserIdno()==null?"":payinfo.getSourceUserIdno()).append(CRT001);
    	//来源
    	sb.append(payinfo.getSource()==null?"":payinfo.getSource()).append(CRT001);
    	//支付宝登陆类型
    	sb.append(payinfo.getLoginType()==null?"":payinfo.getLoginType()).append(CRT001);
    	//支付宝用户类型
    	sb.append(payinfo.getAlipayAccountType()==null?"":payinfo.getAlipayAccountType()).append(CRT001);
    	//消息通知类型
    	sb.append(messageType.getType()==null?"":messageType.getType()).append(CRT001);
    	//余额宝余额
    	sb.append(asset.getYuebaoAmount()==null?"":asset.getYuebaoAmount()).append(CRT001);
    	//余额宝累计收益
    	sb.append(asset.getYuebaoIncome()==null?"":asset.getYuebaoIncome()).append(CRT001);
    	//支付宝余额
    	sb.append(asset.getZhifubaoAmount()==null?"":asset.getZhifubaoAmount()).append(CRT001);
    	//招财宝余额
    	sb.append(asset.getZhaocaibaAmount()==null?"":asset.getZhaocaibaAmount()).append(CRT001);
    	//花呗的授信额度
    	sb.append(asset.getHuabeiLimit()==null?"":asset.getHuabeiLimit()).append(CRT001);
    	//花呗的账单额度 已用额度
    	sb.append(asset.getHuabeiUsed()==null?"":asset.getHuabeiUsed()).append(CRT001);
    	//花呗的本期账单
    	sb.append(asset.getHuabeiBill()==null?"":asset.getHuabeiBill()).append(CRT001);
    	 
    	//是否结束戳
    	sb.append(job.isFinished()==null?"":job.isFinished()).append(CRT001);
    	//是否请求
    	sb.append(job.isRequested()==null?"":job.isRequested()).append(CRT001);
    	//是否成功
    	sb.append(successful).append(CRT001);
    	//起始时间
    	sb.append(job.getStartTime()==null?"":DateUtil.formateDateStr(job.getStartTime())).append(CRT001);
    	//结束时间
    	sb.append(job.getEndTime()==null?"":DateUtil.formateDateStr(job.getEndTime())).append(CRT001);
    	//系统ID
    	sb.append(job.getSystemID()).append(CRT001);
    	//查询编号
    	sb.append(job.getQuery()).append(CRT001);
    	//尝试次数
    	sb.append(job.getTryTimes()==null?"0":job.getTryTimes()).append(CRT001);
    	//JOB类型
    	sb.append("").append(CRT001);
    	//结果信息
    	sb.append("").append(CRT001);
    	//结果码
    	sb.append("");
    	key.set("alipayBase");
    	val.set(sb.toString().replace("\n", " "));
    	context.write(key, val);
    	sb.delete(0,sb.length());
    	sb.append(job_id).append(CRT001).append(dataJson).append(CRT001).append(job.getSystemID());
    	key.set("alipay_datajson");
    	val.set(sb.toString().replace("\n", " "));
    	context.write(key, val);
    	parseAlipayOrder(context,payinfo);
	}
	
 
	/**
	 * 支付宝交易记录数据
	 * @param call
	 * @throws InterruptedException 
	 * @throws IOException 
	 */
	public   void parseAlipayOrder(Context context, AliPayInfo payinfo) throws IOException, InterruptedException{
		
    	StringBuilder sb = new StringBuilder(200);
    	List<AliPayOrder> list = payinfo.getOrders();
    	
    	String s="";
    	
    	for(AliPayOrder o :list){
    		 
    		sb.delete(0, sb.length());
    		
    		//时间戮
    		sb.append(timestamstr).append(CRT001);
    		//数据平台job_id
    		sb.append(this.job_id).append(CRT001);
    		//支付宝账号
    		sb.append(this.account_name).append(CRT001);
    		//交易金额
    		sb.append(o.getAmount()==null?"":o.getAmount()).append(CRT001);
    		//交易时间
    		sb.append(o.getTime()==null?"":DateUtil.formateDateStr(o.getTime())).append(CRT001);
    		//订单号
    		sb.append(o.getOrderNo()==null?"":o.getOrderNo()).append(CRT001);
    		//交易状态
    		sb.append(o.getTradeStatus()==null?"":o.getTradeStatus()).append(CRT001);
    		//交易对方
    		sb.append(o.getOtherSide()==null?"":o.getOtherSide()).append(CRT001);
    		//交易名称
    		sb.append(o.getTradeName());
    		s=sb.toString().replace("\n", " ");
    		s=MD5Util.getMd5(s)+CRT001+s;
    		key.set("alipayOrder");
        	val.set(s);
        	context.write(key, val);
    	}
    	
	}
}
